﻿/*
 * TcpEpoll.h
 *
 */
#ifndef __GDK_TCPEPOLL_H__
#define __GDK_TCPEPOLL_H__

#include <gdk/core/Types.h>
#include <gdk/core/Task.h>
#include <gdk/core/Buffer.h>
#include <gdk/core/Semaphore.h>
#include <gdk/net/TcpConnection.h>
#include <vector>
#include <map>

#if defined(__IOS__)
#	include <sys/types.h>
#	include <sys/event.h>
#	include <sys/time.h>
#elif defined(__GNUC__) || defined(__ANDROID__)
#	include <sys/epoll.h>
#endif



namespace gdk
{



////////////////////////////////////////////////
class GDK_NET_EXPORT TcpEpoll : public Task
{
	DECLARE_CLASS_HANDLE(TcpEpoll)
	DECLARE_CLASS_INSTANCE1(TcpEpoll, Semaphore *)


	typedef struct
	{
		u_int	fd_count;
		SOCKET	fd_array[1];
	} Fd_set;

	typedef std::vector<TcpConnection::Handle> TcpConnectionList;
	TcpConnectionList	_connectionList;
	Semaphore::Handle	_semaphore;

#if defined(_WIN32)

	Buffer				_readfds;
	Buffer				_writefds;

#elif defined(__IOS__)

	int					_kqfd;
	int					_maxevents;
	Buffer				_events;

#elif defined(__GNUC__) || defined(__ANDROID__)

	typedef std::map<SOCKET, TcpConnection::Handle> TcpConnectionMap;
	int					_maxevents;
	int					_epfd;
	Buffer				_events;
	TcpConnectionMap	_mapConnections;

#endif


private:
	TcpEpoll(Semaphore *semaphore);
	~TcpEpoll(void);

public:
	bool add(const TcpConnection::Handle &conn);
	void remove(const TcpConnection::Handle &conn);

public:
	virtual void onTick(uint32_t timeSinceLastTick);
};




}//namespace gdk



#endif /*__GDK_TCPEPOLL_H__*/
